<?php
    ini_set("display_errors", "On");
    error_reporting(E_ALL | E_STRICT);
    require_once(dirname(__FILE__).'/common/config.php');
    /*操作方法*/
    $act = isset($_GET['act']) ? trim($_GET['act']) : '';
    /*获取礼品列表*/
    if ($act == 'getgoodslist') 
    {
        /*列表*/
        $lists = array();
        $rows = $rnpdo->RnFetchAll("select id,name,prize,num,version from {$studentRites} ");
        if($rows['errcode'] == 1 && !empty($rows['errmsg']))
        {
            $lists = $rows['errmsg'];
            die(returnJson('获取成功！',0,$lists));
        }
        else
        {
            die(returnJson('获取失败！',500,$lists));
        }
    }
    /*充值*/
    elseif($act == 'recharge')
    {
        /*充值类型0.卡密提取1.话费充值2.游戏Q币充值*/
        $goodstype = isset($_POST['goodstype']) ? intval($_POST['goodstype']) : 0;
        /*礼品id*/
        $goods_id = isset($_POST['goods_id']) ? intval($_POST['goods_id']) : 0;
        /*用户标识*/
        $userid = isset($_POST['userid']) ? trim($_POST['userid']) : '';
        /*账号/手机号*/
        $game_userid = isset($_POST['game_userid']) ? trim($_POST['game_userid']) : '';
        /*手机号充值*/
        $cardid = '';
        if($goodstype == 1)
        {
            if(!checkMobile($game_userid))
            {
                die(returnJson('手机号格式不正确！'));
            }
            $cardid = '140101';
        }
        /*游戏充值*/
        elseif($goodstype == 2)
        {
            $cardid = '220612';
        }
        /*卡密提取*/
        else
        {
            $cardid = '1240413';
        }
        /*判断用户是否已领取过礼品*/
        $row = $rnpdo->RnFetchRow("select id from {$ritesLog} where `userid`=:userid limit 1 ",array('userid'=>$userid));
        /*if($row['errcode'] == 1 && !empty($row['errmsg']))
        {
            die(returnJson('您已领取礼品！'));
        }*/
        /*判断礼品是否还有库存*/
        $row = $rnpdo->RnFetchRow("select id,name,prize,num,version from {$studentRites} where `id`=:goods_id limit 1 ",array('goods_id'=>$goods_id));
        if(empty($row['errmsg']))
        {
            die(returnJson('礼品已领完！'));
        }
        /*礼品信息*/
        $goods_info = $row['errmsg'];
        if($goods_info['num'] <= 0)
        {
            die(returnJson('礼品已领完！'));
        }
        /*开启事务*/
        $rnpdo->RnBegin();
        /*添加领取记录*/
        /*订单号*/
        $sporder_id = 'snzanca'.date('YmdHis').mt_rand(1000,9999);
        /*充值金额*/
        $cardnum = $goods_info['prize'];
        /*添加时间*/
        $sporder_time = date('YmdHis');
        /*添加数据*/
        $arr = array(
            'sporder_id'=>$sporder_id,
            'game_userid'=>$game_userid,
            'cardnum'=>$cardnum,
            'cardid'=>$cardid,
            'sporder_time'=>$sporder_time,
            'addtime'=>time(),
            'add_date'=>date('Y-m-d'),
            'userid'=>$userid,
            'goods_id'=>$goods_id,
            'goodstype'=>$goodstype
        );
        $order_res = $rnpdo->RnPtmQuery("insert into {$ritesLog} (sporder_id,game_userid,cardnum,cardid,sporder_time,addtime,add_date,userid,goods_id,goodstype) values (:sporder_id,:game_userid,:cardnum,:cardid,:sporder_time,:addtime,:add_date,:userid,:goods_id,:goodstype)", $arr);
        /*更改礼品库存*/
        $goods_update = $rnpdo->RnPtmQuery("update {$studentRites} set num=:num,version=:new_version where `id`=:id and version=:old_version ",array('num'=>$goods_info['num']-1,'new_version'=>$goods_info['version']+1,'id'=>$goods_info['id'],'old_version'=>$goods_info['version']));
        if(empty($order_res['errmsg']) || empty($goods_update['errmsg']))
        {
            $rnpdo->RnRollBack();
            die(returnJson('添加领礼品记录失败！'));
        }
        $rnpdo->RnCommit();
        /*手机充值*/
        if($goodstype == 1)
        {
            require_once(dirname(__FILE__).'/common/mobilerecharge.php');
            /*实例化充值类*/
            $recharge = new mobilerecharge();
            $recharge_res = $recharge->payorder($game_userid,$sporder_id,$cardnum,$cardid,$sporder_time);
            //print_r($recharge_res);die;
            /*日志类型*/
            $log_type = 'mobilere';
        }
        /*游戏充值*/
        elseif($goodstype == 2)
        {
            require_once(dirname(__FILE__).'/common/gamerecharge.php');
            /*实例化充值类*/
            $recharge = new gamerecharge();
            $recharge_res = $recharge->payorder($game_userid,$sporder_id,$cardnum,$cardid,$sporder_time);
            /*日志类型*/
            $log_type = 'gamere';
        }
        /*卡密提取*/
        else
        {
            require_once(dirname(__FILE__).'/common/extract.php');
            /*实例化充值类*/
            $recharge = new extract();
            $recharge_res = $recharge->pickUp($sporder_id,$cardnum,$cardid,$sporder_time);
            /*日志类型*/
            $log_type = 'kmtq';
        }
        /*将结果写入库中*/
        $recharge_res_arr = json_decode($recharge_res,true);
        /*将返回结果写入日志*/
        write_log($recharge_res_arr,$log_type);
        /*欧飞返回信息*/
        $res_arr = isset($recharge_res_arr['info']) ? $recharge_res_arr['info'] : '';
        /*欧飞流水号*/
        $orderid = isset($res_arr['orderid']) ? $res_arr['orderid'] : '';
        /*业务码*/
        $retcode = isset($res_arr['retcode']) ? $res_arr['retcode'] : '';
        /*返回码*/
        $code = isset($recharge_res_arr['code']) ? $recharge_res_arr['code'] : 500;
        /*返回信息*/
        $msg = isset($recharge_res_arr['msg']) ? $recharge_res_arr['msg'] : '参数错误！';
        /*返回结果集*/
        $res = json_encode($res_arr);
        $rnpdo->RnPtmQuery("update {$ritesLog} set retcode=:retcode,res=:res,orderid=:orderid where `sporder_id`=:sporder_id ",array('retcode'=>$retcode,'res'=>$res,'orderid'=>$orderid,'sporder_id'=>$sporder_id));
        if($goodstype == 0)
        {
            require_once(dirname(__FILE__).'/common/sms.php');
            $SendSms = new SendSms();
            $mobile        = '15829337275'; //接收短信的手机号码
            $boc           = 'SN'; //行标示
            $salt          = 'OISIOFIOSL'; //约定生成签名的盐值
            $signName      = '闰能科技'; //短信签名
            $templateCode  = 'SMS_152284975'; //短信模板ID
            $templateParam = array('info' => '测试', 'remark' => '警戒值'); //短信模板参数，数组键名必须与短信模板中的变量名一致
            $remark        = '测试'; //备注，用于区分活动
            $result        = '';//$SendSms->requestApi($mobile, $boc, $salt, $signName, $templateCode, $templateParam, $remark); //调用SendSms的requestApi方法发送短信
            print_r($result); //打印发送结果信息
            die(returnJson($msg,$code,$res_arr));
        }
        if($code == 1)
        {
            die(returnJson('礼品领取成功！',$code));
        }
        else
        {
            die(returnJson($msg,$code));
        }
    }
    